package com.kylin.module.baseapp.actions;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.Order;

import com.kylin.common.Constants;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.PropertyCondition;
import com.kylin.module.exhibition.orm.Exhibition;
import com.kylin.module.shop.orm.Company;
import com.kylin.module.shop.orm.Content;
import com.kylin.module.shop.orm.Product;
import com.kylin.module.specials.orm.Special;
import com.kylin.module.study.orm.Courseware;
import com.kylin.struts.BaseDispatchAction;

/**
 * 网站首页获取数据的服务接口程序
 * 
 * @author lzh
 * 
 */
public class HomeIndexAction extends BaseDispatchAction {

	protected static Logger log = Logger.getLogger(HomeIndexAction.class);

	/**
	 * 最新贸易信息列表
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward newContentList(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		log.debug("显示首页的 最新贸易信息列表 ");
		String forward = "newContentList";
		int pageSize = 5;// 分页列表的每页行数
		int pageNum = 1; // 当前页号
		CriteriaInfo cinfo = null;
		try {
			List condsList = new LinkedList();
			Locale language = getSessionLocale(request);
			PropertyCondition cond = new PropertyCondition("language", language.toString()
					, PropertyCondition.OPERATOR_EQ);
		 	condsList.add(cond);
			cinfo = new CriteriaInfo(Content.class, condsList);
			cinfo.setOrderby(Order.desc("createdon"));
			DisplayPage page = PageManager.getDisplayPage(pageSize, pageNum,cinfo);
			request.setAttribute("newContentList", page);
		} catch (Exception e) {
			forward = doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 显示首页的 最新会展信息
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward newExhibitionList(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		log.debug("显示首页的 最新会展信息");
		String forward = "newExhibitionList";
		int pageSize = 4;// 分页列表的每页行数
		int pageNum = 1;// 当前页号
		CriteriaInfo cinfo = null;
		try{
			List condsList = new LinkedList();
			Locale language = getSessionLocale(request);
			PropertyCondition cond = new PropertyCondition("language", language.toString()
					, PropertyCondition.OPERATOR_EQ);
		 	condsList.add(cond);
			cinfo = new CriteriaInfo(Exhibition.class, condsList);
			cinfo.setOrderby(Order.desc("createdon"));
			DisplayPage page = PageManager.getDisplayPage(pageSize, pageNum,cinfo);
			request.setAttribute("newExhibitionList",page);
		} catch (Exception e) {
			forward = doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 显示首页的 新到商贸通会员
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward newCompanyList(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		log.debug("显示首页的 新到商贸通会员 列表 ");
		String forward = "newCompanyList";
		int pageSize = 4;// 分页列表的每页行数
		int pageNum = 1;// 当前页号
		Locale language = getSessionLocale(request);
		CriteriaInfo cinfo = null;
		try {
			List conditions = new ArrayList();
			conditions.add(new PropertyCondition("language",language.toString(), PropertyCondition.OPERATOR_EQ));
			conditions.add(new PropertyCondition("user.active",
					Constants.ACTIVE_ON, PropertyCondition.OPERATOR_EQ));
			
			cinfo = new CriteriaInfo(Company.class,conditions);
			cinfo.setOrderby(Order.desc("createdon"));
			
			DisplayPage page = PageManager.getDisplayPage(pageSize, pageNum, cinfo);
			request.setAttribute("newCompanyList",page);
		} catch (Exception e) {
			forward = doException(request,e,forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 显示首页的热点关注
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward hotSpecialList(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		log.debug("显示首页的 热点关注 ");
		String forward = "hotSpecialList";
		int pageSize = 10;// 分页列表的每页行数
		int pageNum = 1; // 当前页号
		Locale language = getSessionLocale(request);
		CriteriaInfo cinfo = null;
		try {
			// 获得所有热点专题
			List conditions = new ArrayList();
			PropertyCondition cond = new PropertyCondition("active",
					Special.ACTIVE_HOT, PropertyCondition.OPERATOR_EQ);
			conditions.add(cond);
			// 增加语言查询条件
			PropertyCondition cond_lang = new PropertyCondition("language",
					language.toString(), PropertyCondition.OPERATOR_EQ);
			conditions.add(cond_lang);
			cinfo = new CriteriaInfo(Special.class, conditions);
			cinfo.setOrderby(Order.desc("createdon"));
			DisplayPage page = PageManager.getDisplayPage(pageSize, pageNum,
					cinfo);
			request.setAttribute("hotSpecialList", page);
			// 获得热点并头条的专题
			conditions = new ArrayList();
			cond = new PropertyCondition("active", Special.ACTIVE_HOT_TOP,
					PropertyCondition.OPERATOR_EQ);
			conditions.add(cond);
			// 增加语言查询条件
			conditions.add(cond_lang);
			cinfo = new CriteriaInfo(Special.class, conditions);
			cinfo.setOrderby(Order.desc("createdon"));
			page = PageManager.getDisplayPage(1, 1, cinfo);
			request.setAttribute("hot_topSpecial", page);
		} catch (Exception e) {
			forward = doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 显示首页的 世界经济动态
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward newxxxxList(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		log.debug("显示首页的 世界经济动态 列表 ");
		String forward = "newxxxxList";
		return mapping.findForward(forward);
	}

	/**
	 * 显示首页的 跟我说外语
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward topCourseware(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		log.debug("显示首页的 跟我说外语 ");
		String forward = "topCourseware";
		Locale language = getSessionLocale(request);
		CriteriaInfo cinfo = null;
		try {
			List conditions = new ArrayList();
			PropertyCondition cond_lang = new PropertyCondition("language",
					language.toString(), PropertyCondition.OPERATOR_EQ);
			conditions.add(cond_lang);
			PropertyCondition cond = new PropertyCondition("active",
					Courseware.COURSEWARE_TOP, PropertyCondition.OPERATOR_EQ);
			conditions.add(cond);

			cinfo = new CriteriaInfo(Courseware.class, conditions);
			cinfo.setOrderby(Order.desc("createdon"));

			DisplayPage page = PageManager.getDisplayPage(1, 1, cinfo);
			request.setAttribute("topCourseware", page);

		} catch (Exception e) {
			forward = doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 显示首页的 最新产品
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward newProductList(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		log.debug("显示首页的 最新产品 列表 ");
		String forward = "newProductList";
		int pageSize = 18;// 分页列表的每页行数
		int pageNum = 1; // 当前页号
		Locale language = getSessionLocale(request);
		CriteriaInfo cinfo = null;
		try {
			List conditions = new ArrayList();
			PropertyCondition cond_lang = new PropertyCondition("language",
					language.toString(), PropertyCondition.OPERATOR_EQ);
			conditions.add(cond_lang);
			conditions.add(new PropertyCondition("active", Product.ACTIVE_SHOW,
					PropertyCondition.OPERATOR_EQ));
			cinfo = new CriteriaInfo(Product.class, conditions);
			cinfo.setOrderby(Order.desc("lastupdatedon"));
			DisplayPage page = PageManager.getDisplayPage(pageSize, pageNum,
					cinfo);
			request.setAttribute("newProductList", page);
		} catch (Exception e) {
			forward = doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 显示首页的 推荐产品
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward topProductList(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		log.debug("显示首页的 推荐产品 列表 ");
		String forward = "topProductList";
		return mapping.findForward(forward);
	}

}
